<?php

namespace app\admin\model\xilujob;

use think\Model;
use traits\model\SoftDelete;

class EnterpriseInfo extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'xilujob_enterprise_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $append = [
        //'benefit_ids'
    ];

    public static function init() {
        self::beforeUpdate(function($model){
            $changedData = $model->getChangedData();
            if(isset($changedData['address'])) {
                //$model->geo_status = Constants::GEO_STATUS_PARSING;
            }
        });
    }

    public function getBenefitIdsAttr($value, $data) {
        $ids = [];
        foreach ($this->benefits as $benefit) {
            $ids[] = $benefit->id;
        }
        return implode(',',$ids);
    }

    public function user() {
        return $this->belongsTo('User', 'id');
    }

    public function benefits() {
        return $this->belongsToMany('Benefit',EnterpriseBenefit::class,'benefit_id','enterprise_id');
    }

    public function city() {
        return $this->belongsTo('Area', 'city_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function companyType() {
        return $this->belongsTo(CompanyProperties::class, 'company_type_id');
    }

    public function companyScale() {
        return $this->belongsTo(CompanyScale::class, 'company_scale_id');
    }

    public function industryCategory() {
        return $this->belongsTo(IndustryCategory::class, 'industry_category_id');
    }


    
}
